package com.rjgc.title.fillter;

import com.alibaba.fastjson.JSONObject;
import com.rjgc.title.common.constant.RedisConst;
import com.rjgc.title.common.result.Result;
import com.rjgc.title.common.result.ResultCodeEnum;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import reactor.core.publisher.Mono;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

/**
 * @program: title-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-25 08:39
 **/
@Component
public class AuthGlobalInterceptor implements HandlerInterceptor {


    @Autowired
    private RedisTemplate redisTemplate;

    // 匹配路径的工具类
    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
     * 获取用户id
     *
     * @param request
     * @return
     */
    public String getUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (org.springframework.util.StringUtils.isEmpty(token)) {
            // 获取cookie中的token
            Cookie[] cookies = request.getCookies();

            if (cookies != null && cookies.length > 0) {
                token = cookies[0].getValue();
            }
        }

        if (org.springframework.util.StringUtils.isEmpty(token)) {
            return "";
        } else {
            // 存储在redis中的token
            String tokenKey = RedisConst.USER_LOGIN_KEY_PREFIX + token;
            // 获取对象
            String userId = (String) redisTemplate.opsForValue().get(tokenKey);
            return userId;
        }

    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ServerHttpRequest requestMy = (ServerHttpRequest) request;
        String path = requestMy.getPath().toString();
        // 进入拦截器 获取存放在cookie header中的token数据
        // 然后判断当前路径是否包含
        if (antPathMatcher.match("/**/inner/**", path)) {
            // 然后进行给客户端返回错误信息
            return false;
        }
        // 我们将userId放进入请求头中
        HttpSession session = request.getSession();
        session.setAttribute("userId",this.getUserId(request));
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
